package org.jeecg.modules.demo.customer.service.impl;

import org.jeecg.modules.demo.customer.entity.BdCustomer;
import org.jeecg.modules.demo.customer.entity.BdCustbank;
import org.jeecg.modules.demo.customer.mapper.BdCustbankMapper;
import org.jeecg.modules.demo.customer.mapper.BdCustomerMapper;
import org.jeecg.modules.demo.customer.service.IBdCustomerService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 客户基本信息
 * @Author: jeecg-boot
 * @Date:   2024-03-07
 * @Version: V1.0
 */
@Service
public class BdCustomerServiceImpl extends ServiceImpl<BdCustomerMapper, BdCustomer> implements IBdCustomerService {

	@Autowired
	private BdCustomerMapper bdCustomerMapper;
	@Autowired
	private BdCustbankMapper bdCustbankMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BdCustomer bdCustomer, List<BdCustbank> bdCustbankList) {
		bdCustomerMapper.insert(bdCustomer);
		if(bdCustbankList!=null && bdCustbankList.size()>0) {
			for(BdCustbank entity:bdCustbankList) {
				//外键设置
				entity.setIdH(bdCustomer.getId());
				bdCustbankMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BdCustomer bdCustomer,List<BdCustbank> bdCustbankList) {
		bdCustomerMapper.updateById(bdCustomer);
		
		//1.先删除子表数据
		bdCustbankMapper.deleteByMainId(bdCustomer.getId());
		
		//2.子表数据重新插入
		if(bdCustbankList!=null && bdCustbankList.size()>0) {
			for(BdCustbank entity:bdCustbankList) {
				//外键设置
				entity.setIdH(bdCustomer.getId());
				bdCustbankMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		bdCustbankMapper.deleteByMainId(id);
		bdCustomerMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bdCustbankMapper.deleteByMainId(id.toString());
			bdCustomerMapper.deleteById(id);
		}
	}
	
}
